/************************************************************************
 * <br>
 * <ul>
 * <li>版权所有：唐山弘川科技有限公司 2009－2020</li>
 * <li>创建日期：2009-8-14</li>
 * <li>初始作者：刘川</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 ***********************************************************************/
package com.hy.enterprise.framework.persistence.api ;
/*
import java.util.List;

import javax.persistence.Query;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;

import com.hy.enterprise.framework.lang.IHierarchialbe;
import com.hy.enterprise.framework.lang.hierarchy.MemoryTreeNode;
import com.hy.enterprise.framework.persistence.PersistentException;
import com.hy.enterprise.framework.util.logger.SeamLoggerUtil;
import com.vsoft.libra.enterprise.framework.widget.api.tree.ITreeBusinessBean;

*//**
 * <ul>
 * <li>设计作者：刘川</li>
 * <li>设计日期：2009-8-14</li>
 * <li>设计时间：上午11:06:12</li>
 * <li>设计目的：层次结构抽象数据访问对象实现类</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 *//*
public class AbstractHierarchyDao < PersistentEntity extends HierarchyPersistable >
        extends AbstractDao < PersistentEntity > implements
        IHierarchyDao < PersistentEntity >
{
	*//**
	 * 日志记录器
	 *//*
	private static LogProvider	logger	=
	                                      Logging
	                                             .getLogProvider ( AbstractHierarchyDao.class ) ;

	*//**
	 * 构造函数
	 *//*
	public AbstractHierarchyDao ( )
	{
		super ( ) ;
	}

	*//**
	 * 构造函数
	 * 
	 * @param entityClass
	 *//*
	public AbstractHierarchyDao (
	                              Class < PersistentEntity > entityClass )
	{
		super (
		        entityClass ) ;
	}

	*//**
	 * @see com.hy.enterprise.framework.persistence.api.IHierarchyDao#getParentPersistentEntity(com.hy.enterprise.framework.persistence.api.HierarchyPersistable)
	 *//*
	@ SuppressWarnings ( "unchecked" )
	public PersistentEntity getParentPersistentEntity (
	                                                    PersistentEntity persistentEntity )
	{
		if ( ( null == persistentEntity )
		     || ( null == persistentEntity
		                                  .getIdentifier ( ) )
		     || ( null == persistentEntity
		                                  .getLeftValue ( ) )
		     || ( null == persistentEntity
		                                  .getRightValue ( ) ) )
		{
			return null ;
		}
		else
		{
			String identifier =
			                    persistentEntity
			                                    .getIdentifier ( ) ;
			StringBuffer jpql =
			                    new StringBuffer ( ) ;
			jpql
			    .append (
			              "SELECT parent " )
			    .append (
			              "FROM "
			                      + persistentEntity
			                                        .getClass ( )
			                      + " AS treeNode " )
			    .append (
			              "FROM "
			                      + persistentEntity
			                                        .getClass ( )
			                      + " AS parent " )
			    .append (
			              "WHERE " )
			    .append (
			              "( treeNode.identifier = :identifier ) AND " )
			    .append (
			              "( treeNode.leftValue BETWEEN parent.leftValue AND parent.leftValue ) AND " )
			    .append (
			              "( parent.leftValue = MAX( parent.leftValue ) )" ) ;
			Query query =
			              this
			                  .getEntityManager ( )
			                  .createQuery (
			                                 jpql
			                                     .toString ( ) ) ;
			query
			     .setParameter (
			                     "identifier" ,
			                     identifier ) ;
			return ( PersistentEntity ) query
			                                 .getSingleResult ( ) ;
		}
	}

	*//**
	 * @see com.hy.enterprise.framework.persistence.api.AbstractDao#persist(com.hy.enterprise.framework.persistence.api.Persistable)
	 *//*
	@ Override
	public boolean persist (
	                         PersistentEntity persistentEntity )
	                                                            throws PersistentException
	{
		if ( null == persistentEntity )
		{
			return false ;
		}
		try
		{
			StringBuffer jpql =
			                    null ;
			Query query =
			              null ;
			PersistentEntity parent =
			                          this
			                              .getParentPersistentEntity ( persistentEntity ) ;
			if ( null == parent )
			{
				// 无法获取父节点，则作为根节点插入
				persistentEntity
				                .setLeftValue ( IHierarchialbe.MIN_LEFT_VALUE ) ;
				persistentEntity
				                .setRightValue ( IHierarchialbe.MIN_RIGHT_VALUE ) ;
				this
				    .getEntityManager ( )
				    .persist (
				               persistentEntity ) ;
				this
				    .getEntityManager ( )
				    .flush ( ) ;
			}
			else
			{
				Integer parentRightValue =
				                           this
				                               .getParentPersistentEntity (
				                                                            persistentEntity )
				                               .getRightValue ( ) ;
				this
				    .getEntityManager ( )
				    .getTransaction ( )
				    .begin ( ) ;
				jpql =
				       new StringBuffer ( ) ;
				jpql
				    .append (
				              "UPDATE " )
				    .append (
				              persistentEntity
				                              .getClass ( )
				                      + " AS treeNode " )
				    .append (
				              "SET treeNode.rightValue = treeNode.rightValue + 2 " )
				    .append (
				              "WHERE treeNode.rightValue > :rightValue" ) ;

				query =
				        this
				            .getEntityManager ( )
				            .createQuery (
				                           jpql
				                               .toString ( ) ) ;

				query
				     .setParameter (
				                     "rightValue" ,
				                     parentRightValue ) ;
				query
				     .executeUpdate ( ) ;
				jpql =
				       new StringBuffer ( ) ;
				jpql
				    .append (
				              "UPDATE " )
				    .append (
				              persistentEntity
				                              .getClass ( )
				                      + " AS treeNode " )
				    .append (
				              "SET treeNode.leftValue = treeNode.leftValue + 2 " )
				    .append (
				              "WHERE treeNode.leftValue > :rightValue" ) ;

				query =
				        this
				            .getEntityManager ( )
				            .createQuery (
				                           jpql
				                               .toString ( ) ) ;
				query
				     .setParameter (
				                     "rightValue" ,
				                     parentRightValue ) ;
				query
				     .executeUpdate ( ) ;
				this
				    .getEntityManager ( )
				    .persist (
				               persistentEntity ) ;
				this
				    .getEntityManager ( )
				    .flush ( ) ;
				this
				    .getEntityManager ( )
				    .getTransaction ( )
				    .commit ( ) ;
			}
			return true ;
		}
		catch ( Exception exception )
		{
			this
			    .getEntityManager ( )
			    .getTransaction ( )
			    .rollback ( ) ;
			return false ;
		}
	}

	*//**
	 * @see com.hy.enterprise.framework.persistence.api.AbstractDao#remove(com.hy.enterprise.framework.persistence.api.Persistable)
	 *//*
	@ Override
	public boolean remove (
	                        PersistentEntity persistentEntity )
	                                                           throws PersistentException
	{
		if ( null == persistentEntity )
		{
			return false ;
		}
		try
		{
			StringBuffer jpql =
			                    null ;
			Query query =
			              null ;
			Integer leftValue =
			                    persistentEntity
			                                    .getLeftValue ( ) ;
			Integer rightValue =
			                     persistentEntity
			                                     .getRightValue ( ) ;
			Integer width =
			                rightValue
			                        - leftValue
			                        + 1 ;
			this
			    .getEntityManager ( )
			    .getTransaction ( )
			    .begin ( ) ;
			jpql =
			       new StringBuffer ( ) ;
			jpql
			    .append (
			              "DELETE FROM " )
			    .append (
			              persistentEntity
			                              .getClass ( )
			                      + " AS treeNode " )
			    .append (
			              "WHERE treeNode.leftValue BETWEEN :leftValue AND :rightValue" ) ;
			query =
			        this
			            .getEntityManager ( )
			            .createQuery (
			                           jpql
			                               .toString ( ) ) ;
			query
			     .setParameter (
			                     "leftValue" ,
			                     leftValue ) ;
			query
			     .setParameter (
			                     "rightValue" ,
			                     rightValue ) ;
			query
			     .executeUpdate ( ) ;
			jpql =
			       new StringBuffer ( ) ;
			jpql
			    .append (
			              "UPDATE " )
			    .append (
			              persistentEntity
			                              .getClass ( )
			                      + " AS treeNode " )
			    .append (
			              "SET treeNode.rightValue = treeNode.rightValue - :width " )
			    .append (
			              "WHERE treeNode.rightValue > :rightValue " ) ;
			query =
			        this
			            .getEntityManager ( )
			            .createQuery (
			                           jpql
			                               .toString ( ) ) ;
			query
			     .setParameter (
			                     "width" ,
			                     width ) ;
			query
			     .setParameter (
			                     "rightValue" ,
			                     rightValue ) ;
			query
			     .executeUpdate ( ) ;
			jpql =
			       new StringBuffer ( ) ;
			jpql
			    .append (
			              "UPDATE " )
			    .append (
			              persistentEntity
			                              .getClass ( )
			                      + " AS treeNode " )
			    .append (
			              "SET treeNode.leftValue = leftValue.rightValue - :width " )
			    .append (
			              "WHERE treeNode.leftValue > :rightValue " ) ;
			query =
			        this
			            .getEntityManager ( )
			            .createQuery (
			                           jpql
			                               .toString ( ) ) ;
			query
			     .setParameter (
			                     "width" ,
			                     width ) ;
			query
			     .setParameter (
			                     "rightValue" ,
			                     rightValue ) ;
			query
			     .executeUpdate ( ) ;
			this
			    .getEntityManager ( )
			    .getTransaction ( )
			    .commit ( ) ;
			return true ;
		}
		catch ( Exception exception )
		{
			this
			    .getEntityManager ( )
			    .getTransaction ( )
			    .rollback ( ) ;
			return false ;
		}
	}

	*//**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-22</li>
	 * <li>设计时间：上午11:18:11</li>
	 * <li>设计目的：将
	 * {@link com.vsoft.libra.enterprise.framework.lang.hierarchy.MemoryTreeNode}
	 * 类型列表转换为
	 * {@link com.vsoft.libra.enterprise.framework.widget.api.tree.ITreeBusinessBean}
	 * 类型列表</li>
	 * </ul>
	 * <ul>
	 * <li><b>注意：这里所获取到的内存树节点列表必须为在数据库中按照树节点层次值、节点左值正向排序排好的列表</b></li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param memoryTreeNodes
	 * @return
	 *//*
	protected List < ITreeBusinessBean > queryResultToTreeBusinessBean (
	                                                                     List < Object > memoryTreeNodes )
	{
		if ( ( null == memoryTreeNodes )
		     || ( memoryTreeNodes
		                         .size ( ) == 0 ) )
		{
			return null ;
		}
		else
		{
			Long treeNodeDepth =
			                     ( ( MemoryTreeNode ) memoryTreeNodes
			                                                         .get ( 0 ) )
			                                                                     .getTreeNodeDepth ( ) ;
			for ( Object node : memoryTreeNodes )
			{
				SeamLoggerUtil
				              .debug (
				                       AbstractHierarchyDao.logger ,
				                       "当前所获取到的节点为：/n["
				                               + ToStringBuilder
				                                                .reflectionToString (
				                                                                      node ,
				                                                                      ToStringStyle.MULTI_LINE_STYLE )
				                               + "/" ) ;
				MemoryTreeNode memoryTreeNode =
				                                ( MemoryTreeNode ) node ;
				if ( treeNodeDepth == memoryTreeNode
				                                    .getTreeNodeDepth ( ) )
				{
					// 说明为所查询出的结果树的第一个节点
				}
			}
			return null ;
		}
	}
}*/
